<template>
	<div v-loading="loading">
		<HyForm :hyFormDefault="basicInfoDefault" :hyFormConfig="hyFormConfig" :hyFormFields="hyFormFields" ref="hyFormRef" />
		<HyArrayForm
			:hyArrayFormDefault="integratedApplicationListDefault"
			:hyArrayFormConfig="hyArrayFormConfig"
			:title="'拟整合应用'"
			ref="hyArrayFormRef"
			:hyArrayFormFields="hyArrayFormFields"
		/>
	</div>
	<ChooseApp :visible="visible" @close="close" />
</template>
<script setup lang="ts">
import HyForm from '/@/components/HyForm/index.vue';
import HyArrayForm from '/@/components/HyArrayForm/index.vue';
import { useDict } from '/@/hooks/dict';
import ChooseApp from '../ChooseApp/index.vue';
import { defaultValue } from '../../utils/default';
import { Search } from '@element-plus/icons-vue';
const { basicInfoDefault, integratedApplicationListDefault } = defaultValue();
const formType: any = inject('formType');

// 定义字典
const {
	is_work_team,
	construct_type,
	belong_domain,
	deploy_ports,
	digital_party_building,
	digital_government_affairs,
	digital_economy,
	digital_society,
	digital_culture,
	digital_legal_system,
	grassroots_intelligent_governance,
	team_leader_unit,
} = useDict(
	'is_work_team',
	'construct_type',
	'belong_domain',
	'deploy_ports',
	'digital_party_building',
	'digital_government_affairs',
	'digital_economy',
	'digital_society',
	'digital_culture',
	'digital_legal_system',
	'grassroots_intelligent_governance',
	'team_leader_unit'
);
//配置基本信息表单
const hyFormFields = computed(() => {
	return {
		threeListId: {
			type: 'input',
			hidden: true,
		},
		threeListCode: {
			type: 'input',
			hidden: true,
		},
		threeListName: {
			label: '应用名称',
			type: 'input',
			required: true,
			append: true,
			disabled: true,
			icon: Search,
			appendClick: () => applyName(),
		},
		applicationCode: {
			label: '应用编码',
			type: 'input',
			hidden: true,
		},
		responsibleDeptName: {
			label: '责任部门',
			type: 'input',
			required: true,
			disabled: true,
			placeholder: '请选择应用名称',
		},
		responsibleDept: {
			label: '责任部门ID',
			type: 'input',
			hidden: true,
		},
		headName: {
			label: '负责人姓名',
			type: 'input',
			required: true,
		},
		headPhone: {
			label: '负责人电话',
			type: 'input',
			required: true,
			maxlength: 11,
			rules: [
				{
					pattern: /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/,
					message: '请输入正确的手机号码格式',
					trigger: 'blur',
				},
			],
		},
		belongDomain: {
			label: '所属领域',
			type: 'select',
			required: true,
			options: belong_domain.value,
			placeholder: '请先选择应用名称',

			disabled: true,
		},
		belongingRunway: {
			label: '所属跑道',
			type: 'select',
			required: true,
			placeholder: '请先选择应用名称',
			disabled: true,
			options: belongingRunway.value,
		},
		teamLeaderUnit: {
			label: '专题组长单位',
			type: 'select',
			required: true,
			disabled: true,
			placeholder: '请先选择应用名称',
			options: team_leader_unit.value,
		},
		deployPorts: {
			label: '拟部署端口',
			type: 'select',
			options: deploy_ports.value,
			required: true,
		},
		isWorkTeam: {
			label: '组建工作专班',
			type: 'select',
			options: is_work_team.value,
			required: true,
			change: (option: any) => workTeamChange(option),
		},
		budgetAmount: {
			label: '预算金额',
			required: true,
			type: 'inputFunny',
		},
		constructType: {
			label: '建设类型',
			type: 'select',
			options: construct_type.value,
			required: true,
			multiple: true,
			change: (option: any) => change(option),
		},
		existingApplicationName: {
			label: '已有应用名称',
			type: 'input',
			required: true,
			remove: removeHasApp.value,
		},
		specialTeamFormation: {
			label: '专班组建情况',
			type: 'textarea',
			required: true,
			remove: !isTeam.value,
			place: '请输入“横向贯通涉及部门、纵向贯通所有层级”的工作专班组建情况',
		},
	};
});
const props = defineProps({
	basicInfoData: {
		type: Object,
		default: () => defaultValue().basicInfoDefault,
	},
	integratedApplicationListData: {
		type: Array,
		default: () => defaultValue().integratedApplicationListDefault,
	},
	routerParams: {
		type: Object,
		default: () => {
			return {};
		},
	},
});
const loading = ref(false);

watch(
	[() => props.basicInfoData, () => props.integratedApplicationListData],
	(nVal, oVal) => {
		if (nVal[0]) {
			nextTick(() => {
				const basicInfoData = JSON.parse(JSON.stringify(nVal[0]));
				loading.value = true;
				hyFormRef.value.hyForm = basicInfoData;
				//建设类型触发显示
				if (basicInfoData.constructType.includes('2') || basicInfoData.constructType.includes('4')) {
					removeHasApp.value = false;
				}
				//组建工作专班触发显示
				if (basicInfoData.isWorkTeam) {
					workTeamChange(basicInfoData.isWorkTeam);
				}
				//根据所属领域，设置所属跑道的下拉选项
				if (basicInfoData.belongDomain) {
					setTimeout(() => {
						belongDomainChange(basicInfoData.belongDomain, basicInfoData.teamLeaderUnit, basicInfoData.belongingRunway);
					}, 10);
				}
				loading.value = false;
			});
		}
		if (nVal[1] !== oVal[1]) {
			nextTick(() => {
				hyArrayFormRef.value.cancel();
				nVal[1].forEach((item: any) => {
					hyArrayFormRef.value.add(item);
				});
			});
		}
	},
	{ deep: true }
);
//选择应用
const visible = ref(false);
const applyName = () => {
	visible.value = true;
};
//关闭应用
const close = (data: any) => {
	if (data) {
		belongDomainChange(data.applyFieldCq);
		hyFormRef.value.hyForm = {
			...hyFormRef.value.hyForm,
			threeListName: data.appName,
			applicationCode: data.appCode,
			responsibleDeptName: data.buildOrgName,
			responsibleDept: data.buildOrgId,
			belongDomain: data.applyFieldCq,
			belongingRunway: data.applyFieldCqRunway,
		};
	}
	visible.value = false;
};
//动态表单配置
const hyFormConfig = computed(() => {
	return {
		labelWidth: '120px',
		span: 8,
		offset: 0,
		type: formType.value,
	};
});
//动态表单配置
const hyArrayFormFields = computed(() => {
	return {
		integratedApplicationName: {
			label: '拟整合应用名称',
			type: 'input',
			required: true,
		},
		competentDepartment: {
			label: '主管部门',
			type: 'input',
			required: true,
		},
	};
});

const hyArrayFormConfig = computed(() => {
	return {
		labelWidth: '120px',
		span: 8,
		offset: 0,
		type: formType.value,
	};
});
//建设类型选择
const removeHasApp = ref(true);
const hyArrayFormRef = ref();
const change = (selectArray: string[]) => {
	//迭代
	if (selectArray.includes('2')) {
		if (selectArray.length === 1) {
			hyArrayFormRef.value.cancel();
		}
		construct_type.value = construct_type.value.map((item: any) => {
			if (item.value === '2' || item.value === '3') {
				item.disabled = false;
			} else {
				item.disabled = true;
			}
			return item;
		});
	}
	//新建
	if (selectArray.includes('1')) {
		construct_type.value = construct_type.value.map((item: any) => {
			if (item.value === '1') {
				item.disabled = false;
			} else {
				item.disabled = true;
			}
			return item;
		});
		removeHasApp.value = true;
	}
	if (!selectArray.includes('1')) {
		removeHasApp.value = false;
	}
	//迁移
	if (selectArray.includes('4')) {
		construct_type.value = construct_type.value.map((item: any) => {
			if (item.value === '4') {
				item.disabled = false;
			} else {
				item.disabled = true;
			}
			return item;
		});
		return;
	}
	//整合
	if (selectArray.includes('3')) {
		if (selectArray.length === 1) {
			removeHasApp.value = true;
		}
		hyArrayFormRef.value.cancel();
		hyArrayFormRef.value.add();
		construct_type.value = construct_type.value.map((item: any) => {
			if (item.value === '3' || item.value === '2') {
				item.disabled = false;
			} else {
				item.disabled = true;
			}
			return item;
		});
	}
	if (selectArray.length === 0 || !selectArray) {
		construct_type.value = construct_type.value.map((item: any) => {
			item.disabled = false;
			return item;
		});
		removeHasApp.value = true;
		hyArrayFormRef.value.cancel();
	}
};
//专班组选择
const isTeam = ref(false);
const workTeamChange = (option: string) => {
	if (option === '1') {
		isTeam.value = true;
	} else {
		isTeam.value = false;
	}
};
const hyFormRef = ref();
const getData = () => {
	const formData = hyFormRef.value.hyForm;
	const arrayFormData = hyArrayFormRef.value.submit();
	return {
		formData,
		arrayFormData,
	};
};
const check = async () => {
	const result = await hyFormRef.value.check();
	const result1 = await hyArrayFormRef.value.check();
	if (result && result1) return true;
	return false;
};
//新增三张清单，清除建设类型选择
onMounted(() => {
	if (props.routerParams.operateType === 'add') {
		change([]);
	}
});
//所属领域选择
const belongingRunway = ref([]);
const belongDomainChange = (option: string, teamLeaderUnit_ = '', belongingRunway_ = '') => {
	hyFormRef.value.hyForm.teamLeaderUnit = teamLeaderUnit_;
	hyFormRef.value.hyForm.belongingRunway = belongingRunway_;
	switch (option) {
		case '1':
			belongingRunway.value = digital_government_affairs.value;
			break;
		case '2':
			belongingRunway.value = digital_economy.value;
			break;
		case '3':
			belongingRunway.value = digital_society.value;
			break;
		case '4':
			belongingRunway.value = digital_party_building.value;
			break;
		case '5':
			belongingRunway.value = digital_culture.value;
			break;
		case '6':
			belongingRunway.value = digital_legal_system.value;
			break;
		case '7':
			belongingRunway.value = grassroots_intelligent_governance.value;
			break;
		default:
			break;
	}
	const teamLeaderUnit = team_leader_unit.value.find((item: any) => item.value === option);
	if (teamLeaderUnit) {
		hyFormRef.value.hyForm.teamLeaderUnit = teamLeaderUnit.value;
	}
};

//恢复初始化
defineExpose({ getData, check });
</script>
<style scoped lang="scss"></style>
